package com.sqrj.phoneorder.Controller;

import com.sqrj.phoneorder.Vo.SysUserVo;
import com.sqrj.phoneorder.common.JsonData;
import com.sqrj.phoneorder.model.SysUser;
import com.sqrj.phoneorder.model.WebUserControl;
import com.sqrj.phoneorder.model.WxUnitUserVo;
import com.sqrj.phoneorder.service.SysUserService;
import com.sqrj.phoneorder.service.WebUserControlService;
import com.sqrj.phoneorder.util.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

@RestController
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @Resource
    private WebUserControlService webUserControlService;

    //用户登录
    @PostMapping("webUserLogin")
    public JsonData login(@RequestBody Map<String, Object> map){

        String username = (String)map.get("username");

        String password = (String)map.get("password");

        WebUserControl webUserControl = null;

        SysUser sysUser = sysUserService.findByKeyword(username);

        // code 时间判断
        if(sysUser != null && sysUser.getJbzCode() != null) {
            webUserControl = webUserControlService.findByCode(sysUser.getJbzCode());
        }

        //返回错误信息
        String errorMsg = "";

        if (StringUtils.isBlank(username)) {
            errorMsg = "用户名不可以为空";
            return  JsonData.fail(errorMsg);
        } else if (StringUtils.isBlank(password)) {
            errorMsg = "密码不可以为空";
            return  JsonData.fail(errorMsg);
        } else if (sysUser == null) {
            errorMsg = "查询不到指定的用户";
            return  JsonData.fail(errorMsg);
        } else if (!sysUser.getPassword().equals(MD5Util.encrypt(password))) {
            errorMsg = "用户名或密码错误";
            return  JsonData.fail(errorMsg);
        } else if (sysUser.getStatus() != 1) {
            errorMsg = "账号已冻结，请联系022-66351935开通";
            return  JsonData.fail(errorMsg);
        }else if ( sysUser.getJbzCode() == null ) {
            errorMsg = "该用户未绑定搅拌站！";
            return  JsonData.fail(errorMsg);
        }else if ((!sysUser.getJbzCode().contains("0000")) && webUserControl == null) {
            errorMsg = "查询不到该用户所属搅拌站，请刷新重试";
            return  JsonData.fail(errorMsg);
        } else if ( (!sysUser.getJbzCode().contains("0000")) && webUserControl.getRemaintime().indexOf('-')>-1) {
            errorMsg = "搅拌站已到期，请联系022-66351935开通";
            return  JsonData.fail(errorMsg);
        } else {
            SysUserVo sysUserVo = new SysUserVo();
            //判断用户是否绑定微信
            if(sysUser.getWxopenid() == null) {
                sysUserVo.setBindWX(false);
                sysUserVo.setJbzCode(sysUser.getJbzCode());
            }else{
                sysUserVo.setBindWX(true);
                sysUserVo.setJbzCode(sysUser.getJbzCode());
            }
            return JsonData.success(sysUserVo);

        }

    }
}
